package com.scze.imdemo.myapplication.ui;

import android.Manifest;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.os.Build;
import android.os.Bundle;
import android.support.v4.app.ActivityCompat;
import android.support.v4.content.ContextCompat;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Toast;

import com.hyphenate.EMError;
import com.hyphenate.chat.EMClient;
import com.hyphenate.exceptions.HyphenateException;
import com.scze.imdemo.R;

import static android.widget.Toast.LENGTH_SHORT;

public class MainActivity extends AppCompatActivity {
    private String userName = "picontent";
    private String password = "picontent";
    // 要申请的权限
      private String[] permissions = {Manifest.permission.WRITE_EXTERNAL_STORAGE};
      private AlertDialog dialog;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
       // 版本判断。当手机系统大于 23 时，才有必要去判断权限是否获取
       if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {

             // 检查该权限是否已经获取
             int i = ContextCompat.checkSelfPermission(this, permissions[0]);
             // 权限是否已经 授权 GRANTED---授权  DINIED---拒绝
             if (i != PackageManager.PERMISSION_GRANTED) {
                    // 如果没有授予该权限，就去提示用户请求
                        showDialogTipUserRequestPermission();
             }
       }

        new Thread(){
            @Override
            public void run() {
                try {
                    EMClient.getInstance().createAccount(userName, password);//同步方法
                    System.out.println(userName + "||" + password);
                } catch (HyphenateException e) {
                    e.printStackTrace();
                    switch (e.getErrorCode()) {
                        case EMError.SERVER_BUSY:
                        case EMError.SERVER_UNKNOWN_ERROR:
                        case EMError.SERVER_NOT_REACHABLE:
                        case EMError.SERVER_TIMEOUT:
                        case EMError.NETWORK_ERROR://网络错误
                            showToast("网络有问题,请稍后再试");
                            break;
                        case EMError.USER_ALREADY_EXIST://用户名已存在
                            showToast( "用户名已存在,请尝试登录");
                            break;
                        case EMError.USER_REG_FAILED://注册失败
                            Toast.makeText(getApplicationContext(), "注册失败", Toast.LENGTH_SHORT).show();
                            break;
                        default:
                            break;
                    }
                }
            }
        }.start();
    }
    private void showToast(final String content2){
        runOnUiThread(new Runnable() {
            @Override
            public void run() {
                Toast.makeText(MainActivity.this,content2, LENGTH_SHORT).show();
            }
        });
    }
    public void jin(View v){
        Intent intent = new Intent(this,ChatActivity.class);
        startActivity(intent);
    }

    private void showDialogTipUserRequestPermission() {

          new AlertDialog.Builder(this)
                   .setTitle("存储权限不可用")
                 .setMessage("由于支付宝需要获取存储空间，为你存储个人信息；\n否则，您将无法正常使用支付宝")
                     .setPositiveButton("立即开启", new DialogInterface.OnClickListener() {
                     @Override
                          public void onClick(DialogInterface dialog, int which) {
                           startRequestPermission();
                      }
                       })
                 .setNegativeButton("取消", new DialogInterface.OnClickListener() {
                          @Override
                           public void onClick(DialogInterface dialog, int which) {
                              finish();
                            }
                        }).setCancelable(false).show();
        }

    // 开始提交请求权限
      private void startRequestPermission() {
             ActivityCompat.requestPermissions(this, permissions, 321);
           }
}
